home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5840 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  208 lines

  1. Path: news.InterGate.BC.CA!usenet
  2. From: (tu)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why C++ sucks++
  5. Date: Wed, 07 Feb 1996 03:12:37 GMT
  6. Organization: h
  7. Message-ID: <4f925g$i5f@carrera.intergate.bc.ca>
  8. References: <1996Jan29.223357.1@aspen>
  9. NNTP-Posting-Host: pm4s3.intergate.bc.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. ferriom@aspen wrote:
  13.  
  14.  
  15.  
  16. >*** HIGH SCHOOL ***
  17.  
  18. >100  PRINT "Hello World."
  19. >999  END
  20.  
  21.  
  22.  
  23. >*** COLLEGE FRESHMAN ***
  24.  
  25. >       WRITE (6, 100)
  26. >  100  FORMAT (1X, 'Hello World.')
  27. >       END
  28.  
  29.  
  30.  
  31. >*** ADVANCED COLLEGE ***
  32.  
  33. >PROGRAM P1(INPUT, OUTPUT);
  34.  
  35. >BEGIN
  36. >WRITELN(OUTPUT, 'Hello World.');
  37. >END.
  38.  
  39.  
  40.  
  41. >*** RECENT COLLEGE GRADUATE ***
  42.  
  43. >       IDENTIFICATION DIVISION.
  44. >       PROGRAM-ID.
  45. >           P1.
  46.  
  47. >       ENVIRONMENT DIVISION.
  48.  
  49. >       DATA DIVISION.
  50. >       WORKING-STORAGE SECTION.
  51. >       01 MESSAGE-RECORD                     PICTURE X(12).
  52.  
  53. >       PROCEDURE DIVISION.
  54. >       PROGRAM-BEGIN.
  55. >       FIRST-PARAGRAPH.
  56. >           MOVE 'Hello World.' TO MESSAGE-RECORD.
  57. >           DISPLAY MESSAGE-RECORD.
  58. >       PROGRAM-DONE.
  59. >           STOP RUN.
  60.  
  61.  
  62.  
  63. >*** SEASONED PROFESSIONAL ***
  64.  
  65. >#include <iostream.h>
  66. >#include <string.h>
  67.  
  68.  
  69. >class String
  70. >{
  71. > public:
  72. >  String();
  73. >  String(const char *const);
  74. >  String(const String &);
  75. >  ~String();
  76. >  char & operator[](unsigned short offset);
  77. >  char operator[](unsigned short offset) const;
  78. >  String operator+(const String&);
  79. >  void operator+=(const String&);
  80. >  String & operator= (const String &);
  81. >  unsigned short GetLen()const {return itsLen; }
  82. >  const char * GetString() const {return itsString; }
  83. > private:
  84. >  String (unsigned short);
  85. >  char * itsString;
  86. >  unsigned short itsLen;
  87. >};
  88.  
  89.  
  90. >String::String()
  91. >{
  92. > itsString = new char[1];
  93. > itsString[0] = '\0';
  94. > itsLen = 0;
  95. >}
  96.  
  97. >String::String(unsigned short len)
  98. >{
  99. > itsString = new char[len + 1];
  100. > for (unsigned short i = 0;  i <= len;  i++)
  101. >  itsString[i] = '\0';
  102. > itsLen = len;
  103. >}
  104.  
  105. >String::String(const char * const cString)
  106. >{
  107. > itsLen = strlen(cString);
  108. > itsString = new char[itsLen + 1];
  109. > for (unsigned short i = 0;  i < itsLen;  i++)
  110. >  itsString[i] = cString[i];
  111. > itsString[itsLen] = '\0';
  112. >}
  113.  
  114. >String::String(const String & rhs)
  115. >{
  116. > itsLen = rhs.GetLen();
  117. > itsString = new char[itsLen + 1];
  118. > for (unsigned short i = 0;  i < itsLen;  i++)
  119. >  itsString[i] = rhs[i];
  120. > itsString[itsLen] = '\0';
  121. >}
  122.  
  123. >String::~String()
  124. >{
  125. > delete [] itsString;
  126. > itsLen = 0;
  127. >}
  128.  
  129. >String& String::operator=(const String & rhs)
  130. >{
  131. > if (this == &rhs)
  132. >  return *this;
  133. > delete [] itsString;
  134. > itsLen = rhs.GetLen();
  135. > itsString = new char[itsLen + 1];
  136. > for (unsigned short i = 0;  i < itsLen;  i++)
  137. >  itsString[i] = rhs[i];
  138. > itsString[itsLen] = '\0';
  139. > return *this;
  140. >}
  141.  
  142. >char & String::operator[](unsigned short offset)
  143. >{
  144. > if (offset > itsLen)
  145. >  return itsString[itsLen - 1];
  146. > else
  147. > return itsString[offset];
  148. >}
  149.  
  150. >char String::operator[](unsigned short offset) const
  151. >{
  152. > if (offset > itsLen)
  153. >  return itsString[itsLen - 1];
  154. > else
  155. >  return itsString[offset];
  156. >}
  157.  
  158. >String String::operator+(const String& rhs)
  159. >{
  160. > unsigned short totalLen = itsLen + rhs.GetLen();
  161. > String temp(totalLen);
  162. > for (unsigned short i = 0;  i < itsLen;  i++)
  163. >  temp[i] = itsString[i];
  164. > for (unsigned short j = 0;  j < rhs.GetLen();  j++, i++)
  165. >  temp[i] = rhs[j];
  166. > temp[totalLen] = '\0';
  167. > return temp;
  168. >}
  169.  
  170. >void String::operator+=(const String& rhs)
  171. >{
  172. > unsigned short rhsLen = rhs.GetLen();
  173. > unsigned short totalLen = itsLen + rhsLen;
  174. > String temp(totalLen);
  175. > for (unsigned short i = 0;  i < itsLen;  i++)
  176. >  temp[i] = itsString[i];
  177. > for (unsigned short j = 0;  j < rhs.GetLen();  j++,  i++)
  178. >  temp[i] = rhs[i - itsLen];
  179. > temp[totalLen] = '\0';
  180. > *this = temp; 
  181. >}
  182.  
  183.  
  184. >main()
  185. >{
  186. > String s1("Hello");
  187. > String s2(" ");
  188. > String s3("World.");
  189. > String s4 = s1 + s2 + s3;
  190. > cout << s4.GetString() << endl;
  191. >}
  192.  
  193.  
  194. i don't get your point!!!
  195. to write Hello you could simply write
  196.  
  197. #include <iostream.h>
  198.  
  199. void main()
  200. {
  201.     cout << "Hello" << endl;
  202. }
  203.  
  204. What's the difference from basic, pascal, modula 2, and any other
  205. languages??
  206. DS
  207.  
  208.